<template>
	<view class="tutorEnter">
		<tui-navigation-bar   :titleSize="18" :titleLineHeight="18" :titlefontWeight="800" :isOpacity="false"
			@init="initNavigation" title="教练入驻" backgroundColor="#fff" color="#000">
			<view class="tui-header-icon" :style="{ marginTop: top + 'px' }">
				<tui-icon name="arrowleft" color="#333" @click="back"></tui-icon>
			</view>
		</tui-navigation-bar>
		<view class="tutorEnter_content" :style="{ marginTop: marginTp + 'px' }">
			<!-- 基础信息 -->
			<view class="infoTitle">
				<image class="contentImg" src="/static/user/my/icon_line2@2x.png" mode="aspectFill"></image>
				<view class="contentText"><text>基础信息</text></view>
			</view>
			<view class="inputItem">
				<view class="label"><text>真实姓名</text></view>
				<view class="divide"></view>
				<view class="input">
					<tui-input padding="0" v-model="realName" :size="28" :borderBottom="false" :borderTop="false" placeholder="请输入姓名" clearable></tui-input>
				</view>
			</view>
			<view class="inputItem">
				<view class="label"><text>手机号</text></view>
				<view class="divide"></view>
				<view class="input">
					<tui-input v-model="phoneNumber" padding="0" :size="28" :borderBottom="false" :borderTop="false" placeholder="请输入手机号" clearable ></tui-input>
				</view>
			</view>
			<view class="inputItem">
				<view class="label"><text>身份证号</text></view>
				<view class="divide"></view>
				<view class="input">
					<tui-input  v-model="idNumber" padding="0" :size="28" :borderBottom="false" :borderTop="false" placeholder="请输入身份证号" clearable></tui-input>
				</view>
			</view>
			<view class="inputItem">
				<view class="label"><text>性别</text></view>
				<view class="divide"></view>
				<view class="select">
					<view :class="selectActive ? 'man active' :'man'" @tap="selectActive=true">
						<image v-if="selectActive" class="slectImg" src="/static/user/my/icon_radio1@2x.png"></image>
						<image v-else class="slectImg" src="/static/user/my/icon_radio2@2x.png"></image>
						<view class="manText"><text>男</text></view>
					</view>
					<view :class="!selectActive ? 'girl active' :'girl'" @tap="selectActive=false">
							<image v-if="!selectActive" class="slectImg" src="/static/user/my/icon_radio1@2x.png"></image>
							<image v-else class="slectImg" src="/static/user/my/icon_radio2@2x.png"></image>
							<view class="girlText"><text>女</text></view>
					</view>
				</view>
			</view>
			<!-- 上传身份证图片 -->
			<view class="imgTitle">
				<image class="contentImg" src="/static/user/my/icon_line2@2x.png" mode="aspectFill"></image>
				<view class="contentText"><text>上传图片</text></view>
			</view>
			<view class="uploadPicture">
				<view class="Picture" @tap="uploadImg(true)">
					<image  class="pictureBg" src="/static/user/my/bg_renxiang@2x.png"></image>
					<image  v-if="!frontCard" class="pictureImg" src="/static/user/my/icon_photo@2x.png"></image>
					<image v-else :src="ossUrl+frontCard" class="centerimg"></image>
				</view>
				<view class="Picture"  @tap="uploadImg(false)">
					<image  class="pictureBg" src="/static/user/my/bg_guohui@2x.png"></image>
					<image v-if="!reverseCard"  class="pictureImg" src="/static/user/my/icon_photo@2x.png"></image>
					<image v-else :src="ossUrl+reverseCard" class="centerimg"></image>
				</view>
			</view>
		</view>
		  <view class="footer">
		  		<tui-form-button @click="submitApplications" :loading="isLoading" margin="24rpx 0 0 0"  radius="40rpx" :size="32" color="#fff" width="690rpx" height="84rpx" background="#2FD7D3">
		  			提交申请
		  		</tui-form-button>
		  </view>
	</view>
</template>

<script>
	import {tutorAssistan} from "@/http/api/tutor/tutor.js"
	export default {
		data() {
			return {
				top: 0, //标题图标距离顶部距离
				marginTp: 0,
				isLoading:false,
				ossUrl:getApp().globalData.ossurl,//图片前缀
				realName:'',//姓名
				phoneNumber:'',//手机号
				idNumber:'',//身份证号
				frontCard:'',//身份证正面
				reverseCard:'',//身份证反面
				gender:'',//性别
				selectActive:true //默认男性
			}
		},
		methods: {
			initNavigation(e) {
				this.top = e.top;
				this.marginTp = e.height
			},
			back() {
				uni.navigateBack({
							        delta: 1
							    });;
			},
			//提交申请
			submitApplications(){
					if (!this.realName) {
						this.$utils.toast('请输入姓名');
						return false;
					}
					if (!this.idNumber) {
						this.$utils.toast('请输入身份证号');
						return false;
					}
					if (!this.$regexp.idCard.test(this.idNumber)) {
						this.$utils.toast('请输入正确的身份证号');
						return false;
					}
					if (!this.phoneNumber) {
						this.$utils.toast('请输入手机号');
						return false;
					}
					 if (!this.$regexp.phone.test(this.phoneNumber)) {
						this.$utils.toast('请输入正确的手机号');
						return false;
					}
					if (!this.frontCard) {
						this.$utils.toast('请上传身份证正面');
						return false;
					}
					if (!this.reverseCard) {
						this.$utils.toast('请上传身份证背面');
						return false;
					}
					tutorAssistan({
						realName:this.realName,//姓名
						phoneNumber:this.phoneNumber,//手机号
						idNumber:this.idNumber,//身份证号
						frontCard:this.frontCard,//身份证正面
						reverseCard:this.reverseCard,//身份证反面
						gender:this.selectActive? 0:1,//性别
					}).then(res=>{
						if(res.code===200){
							this.$utils.toast('教练入驻申请提交成功')
							setTimeout(()=>{
								uni.navigateBack({
							        delta: 1
							    });;
							},500)
						}
					})
			},
			//上传图片
			uploadImg(isFrontCard){
				let _this = this;
				uni.showLoading({
					title: '上传中...'
				})

				// 使用通用的选择媒体方法
				_this.$utils.chooseMedia({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					mediaType: ['image']
				}, (res, success) => {
					if (success) {
						// 选择成功，上传文件
						_this.$utils.uploadFile(res.tempFilePaths[0], (imgInfo) => {
							if (imgInfo.statusCode == 200) {
								let dataImg = JSON.parse(imgInfo.data);
								if(isFrontCard){
									_this.frontCard = dataImg.data.fileName;
								} else {
									_this.reverseCard = dataImg.data.fileName;
								}
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none',
									duration: 2000
								});
							}
							uni.hideLoading();
						});
					} else {
						// 选择失败
						uni.hideLoading();
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './tutorEnter.scss';
</style>